package com.test.springboot.utils;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class S40 {
    private int num = 10;
    ReadWriteLock rw = new ReentrantReadWriteLock();

    public void testRead() {
        rw.readLock().lock();
        System.out.println(Thread.currentThread() + "开始读");
        num--;
        System.out.println(Thread.currentThread() + "读结束");
        rw.readLock().unlock();
    }

    public void testWriter() {
        rw.writeLock().lock();
        System.out.println(Thread.currentThread() + "开始写");
        System.out.println(Thread.currentThread() + "写结束");
        rw.writeLock().unlock();
    }


    public static void main(String[] args) {
        S40 s40 = new S40();
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                s40.testRead();
            }).start();
        }
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                s40.testWriter();
            }).start();
            ;
        }
    }
}
